package org.i1510.joeone.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.i1510.common.bean.PageBean;
import org.i1510.common.service.impl.BaseServiceImpl;
import org.i1510.common.utils.BeanUtils;
import org.i1510.common.utils.DateUtils;
import org.i1510.common.utils.IDGenerator;
import org.i1510.common.utils.UserUtils;
import org.i1510.common.utils.web.ResultMapper;
import org.i1510.joeone.dao.IDhVoteDao;
import org.i1510.joeone.po.DhVote;
import org.i1510.joeone.service.IDhVoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("iDhVoteService")
public class DhVoteServiceImpl extends BaseServiceImpl implements IDhVoteService{
    @Autowired
    private IDhVoteDao iDhVoteDao;    
    

    public List getUserStat(DhVote dhVote,Map conditions) {
        Map map = new HashMap();
        //Bean转成Map
        BeanUtils.ObjectCopyMap(dhVote, map);
        map.putAll(conditions);
        List list = this.queryForListBySql("dhVoteSql.getUserStat", map);
        return list;
    }
    public List getStat(DhVote dhVote,Map conditions) {
        Map map = new HashMap();
        //Bean转成Map
        BeanUtils.ObjectCopyMap(dhVote, map);
        map.putAll(conditions);
        List list = this.queryForListBySql("dhVoteSql.getStat", map);
        return list;
    }
    
    public ResultMapper save(DhVote basePo,HttpServletRequest request) {
        ResultMapper mapper = ResultMapper.generateFailure();
        DhVote saved = null;
        if(null !=basePo && basePo.getDocId() != null  && !basePo.getDocId().equals("")){//修改
            DhVote finder = iDhVoteDao.findById(basePo.getDocId());   
            if(null != finder){
                finder.setDocLastTime(DateUtils.getNow());
                finder.setDocLastor(UserUtils.getSessionUserBean(request).getSysUser().getDocId());            
                BeanUtils.copy(finder, basePo);
                saved = iDhVoteDao.save(finder); 
            }else{
                mapper = ResultMapper.generateFailure();
                return mapper;
            }
            
        }else{//新增
            basePo.setDocId(IDGenerator.getGenerateID());
            basePo.setDocCreateTime(DateUtils.getNow());
            basePo.setDocCreator(UserUtils.getSessionUserBean(request).getSysUser().getDocId());
            saved = iDhVoteDao.save(basePo);
        }
        if (saved == null) {
            mapper = ResultMapper.generateFailure();
            mapper.appendResult("id", saved.getDocId());
            return mapper;
        }else{
            mapper = ResultMapper.generateSuccess();
            mapper.appendResult("id", saved.getDocId());
            return mapper;            
        }
    }
    public ResultMapper delete(String docId) {
        ResultMapper mapper = ResultMapper.generateFailure();
        iDhVoteDao.delete(docId);
        mapper = ResultMapper.generateSuccess();
        return mapper;
    }
    
    public DhVote findById(String docId){
        return  iDhVoteDao.findById(docId);
    }
    
    public List<DhVote> findByIds(List<String> docIds){
        return (List<DhVote>) iDhVoteDao.findAll(docIds);
    }
    public List getPage(DhVote dhVote,PageBean pageBean,Map conditions) {
        Map map = new HashMap();
        //Bean转成Map
        BeanUtils.ObjectCopyMap(dhVote, map);
        map.put("intPageIndex", pageBean.getIntPageIndex());
        map.put("intPageSize", pageBean.getIntPageSize());
        map.putAll(conditions);
        List list = this.queryForListBySql("dhVoteSql.getPage", map);
        return list;
    }
    public int getPageCount(DhVote dhVote,Map conditions) {
        Map map = new HashMap();
        //Bean转成Map
        BeanUtils.ObjectCopyMap(dhVote, map);
        map.putAll(conditions);
        List list = this.queryForListBySql("dhVoteSql.getPageCount", map);        
        return list.size();
    }
}
